﻿CREATE PROCEDURE [ServiceEngine].[CreateOperationVersion]
	@OperationID uniqueidentifier,
	@VersionMajor int,
	@VersionMinor int,
	@VersionBuild int,
	@VersionQFE int,
	@Description ntext = null,
	@AutoResponse Xml = null,
	@IsPublic bit = 0,
	@IsActive bit = 0,
	@IsTestable bit = 0,
	@RequestTransform xml = null,
	@ResponseTransform xml = null,
	@ResourceID uniqueidentifier = null,
	@PolicyID UNIQUEIDENTIFIER = null,	
	@ID uniqueidentifier OUTPUT
AS
BEGIN
	DECLARE @ErrorMessage NVARCHAR(4000);
	DECLARE @ErrorSeverity INT;
	DECLARE @ErrorState INT;
	SET NOCOUNT ON;

	BEGIN TRY
		BEGIN TRANSACTION

		SET @ID = newid()

		INSERT INTO ServiceEngine.OperationVersions (OperationVersionID, OperationID, VersionMajor, VersionMinor, VersionBuild, VersionQFE,
					Description, AutoResponse, IsPublic, IsActive, IsTestable, RequestTransformXSLT, ResponseTransformXSLT, ResourceID, PolicyID)
		VALUES (@ID, @OperationID, @VersionMajor, @VersionMinor, @VersionBuild, @VersionQFE,
					@Description, @AutoResponse, @IsPublic, @IsActive, @IsTestable, @RequestTransform, @ResponseTransform, @ResourceID, @PolicyID)

		COMMIT TRANSACTION
	END TRY
	BEGIN CATCH
		SELECT 
			@ErrorMessage = ERROR_MESSAGE(),
			@ErrorSeverity = ERROR_SEVERITY(),
			@ErrorState = ERROR_STATE();

		IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;

		RAISERROR (@ErrorMessage, 
				   @ErrorSeverity,
				   1);
	END CATCH
END


